package frame.laxcen.response;

import frame.laxcen.BaseFrame;
import frame.laxcen.dataobject.DataObject_0001_05;
import frame.laxcen.exception.IllegalFrameFormatException;
import frame.laxcen.response.annotation.FrameFeature;
import frame.laxcen.util.FrameReader;
import frame.util.ByteArrayUtil;
import frame.util.ByteUtil;

import java.util.HashMap;
import java.util.Map;

// 返回读写器IP设置数据
@FrameFeature(MessageType = 0b0001, Mid = 0x05)
public class Response_0001_05 extends BaseResponse {

    public Response_0001_05(BaseFrame baseFrame) throws IllegalFrameFormatException {

        super(baseFrame);

        getContentMap().putAll(explainToMap());
    }

    @Override
    protected Map<String, String> analyzeResponse(FrameReader reader) throws IllegalFrameFormatException {

        Map<String, String> result = new HashMap<>();

        // 读写器 IP

        int[] ip = ByteArrayUtil.toUnsignedIntArray(ByteUtil.intToBytes(reader.readU32()));

        result.put("读写器 IP", String.format("%d.%d.%d.%d", ip[0], ip[1] & 0xff, ip[2] & 0xff, ip[3] & 0xff));

        // 读写器子网掩码

        int[] mask = ByteArrayUtil.toUnsignedIntArray(ByteUtil.intToBytes(reader.readU32()));

        result.put("读写器子网掩码", String.format("%d.%d.%d.%d", mask[0], mask[1], mask[2], mask[3]));

        // 读写器默认网关

        int[] bridge = ByteArrayUtil.toUnsignedIntArray(ByteUtil.intToBytes(reader.readU32()));

        result.put("读写器默认网关", String.format("%d.%d.%d.%d", bridge[0], bridge[1], bridge[2], bridge[3]));

        return result;
    }

    public DataObject_0001_05 getDataObject() {

        return new DataObject_0001_05(getContentMap());
    }
}
